<?php
namespace app\index\controller;
use think\Controller;
use think\Config;
use think\Db;

class Dishes extends Controller
{
	const NUM_PER_PAGE = 5;

	// 上传菜品信息
	public function uploadDishesInfo() {
		$info = $_POST;
		$food = model('Food');
		$foodId = $_POST['food_id'];
		$foodArr = $food->where('id', $foodId)->find();

		$res = array('code' => 0);

		if ($foodArr) {
			$food->where('id', $foodId)->update([
				'food_name' => $info['food_name'],
				'food_img' => $info['food_img'],
				'desc_text' => $info['desc_text'],
				'desc_img1' => $info['desc_img1'],
				'desc_img2' => $info['desc_img2'],
				'desc_img3' => $info['desc_img3'],
				'price' => $info['price'],
				'food_style' => $info['food_style'],
				'food_flavor' => $info['food_flavor'],
				'latitude' => $info['latitude'],
				'longitude' => $info['longitude']
			]);
			$res = array(
				'code' => 1,
				'food_id' => $foodArr->id
			);
		} else {
			if ($this->isUploadFood($info['shop_id'])) {
				$this->addFoodStyle($info['food_style']);
				$food->data([
					'openid' => $info['openid'],
					'food_name' => $info['food_name'],
					'shop_id' => $info['shop_id'],
					'food_img' => $info['food_img'],
					'desc_text' => $info['desc_text'],
					'desc_img1' => $info['desc_img1'],
					'desc_img2' => $info['desc_img2'],
					'desc_img3' => $info['desc_img3'],
					'price' => $info['price'],
					'food_style' => $info['food_style'],
					'food_flavor' => $info['food_flavor'],
					'latitude' => $info['latitude'],
					'longitude' => $info['longitude']
				]);

				if ($food->save()) {
					$res = array(
						'code' => 1, 
						'food_id' => $food->id
					);
				}
			}
		}

		echo json_encode($res);
		exit();
	}


	protected function isUploadFood($shopId) {
		$maxFoodsCount = model('Shop')->where('id', $shopId)->find()->max_foods_count;
		$foodsCount = model('Food')->where('shop_id', $shopId)->count();
		if ($foodsCount >= $maxFoodsCount) {
			return false;
		}
		return true;
	}

	// 添加食品类别
	protected function addFoodStyle($foodStyle) {
		$foodStyles = model('FoodStyles');
		if (!($foodStyles->where('food_style', $foodStyle)->find())) {
			$foodStyles->data([
				'food_style' => $foodStyle
			]);
			$foodStyles->save();
		}
	}

	// 获取菜品信息
	public function getFoodInfo() {
		$foodId = $_POST['food_id'];
		$food = model('Food')->where('id', $foodId)->find();
		$res = array('code' => 0);
		$openid = $_POST['openid'];
		if ($food) {
			$res = array(
				'code' => 1,
				'openid' => $food->openid,
				'food_name' => $food->food_name,
				'shop_id' => $food->shop_id,
				'food_img' => $food->food_img,
				'desc_text' => $food->desc_text,
				'desc_img1' => $food->desc_img1,
				'desc_img2' => $food->desc_img2,
				'desc_img3' => $food->desc_img3,
				'price' => $food->price,
				'food_style' => $food->food_style,
				'star_score' => $food->star_score,
				'food_flavor' => $food->food_flavor,
				'collect_num' => $food->collect_num,
				'latitude' => $food->latitude,
				'longitude' => $food->longitude
			);
			$res['is_collect'] = model('CollectFood')->where(['food_id' => $foodId, 'openid' => $openid])->find() ? 1 : 0; 
		} 
		echo json_encode($res);
		exit();
	}

	// 获取一个商户中的所有菜品
	public function getAllFoodsInShop() {
		$shopId = $_POST['shop_id'];
		$foods = model('Food')->where('shop_id', $shopId)->order('update_time', 'desc')->select();
		echo json_encode($foods);
		exit();
	}

	public function getFoodsCountInShop() {
		$shopId = $_POST['shop_id'];
		$count = model('Food')->where('shop_id', $shopId)->count();
		echo json_encode($count);
		exit();
	}

	// 删除一个菜品
	public function deleteFood() {
		$foodId = $_POST['food_id'];
		
		$arr = array('code' => 0);
		$res = model('Food')->where('id', $foodId)->delete();
		if ($res) {
			$arr = array('code' => 1);
		}
		echo json_encode($arr);
		exit();
	}


	public function getFoods() {
		$latitude = $_POST['latitude'];
		$longitude = $_POST['longitude'];

		$page = $_POST['page'];

		$openid = $_POST['openid'];
		// $orderRule = $_POST['order_rule'];
		$start = ($page - 1) * self::NUM_PER_PAGE;
		$perPageNum = self::NUM_PER_PAGE;

		$orderRule = $_POST['order_rule'];

		$searchKeyWord = $_POST['key_word'];

		$foodStyle = $_POST['food_style'];
		$condition = strlen($foodStyle) == 0 ? "" : "WHERE food_style = '{$foodStyle}'";

		$order = ($orderRule == 'distance') || ($orderRule == 'price_asc') ? 'asc' : 'desc';

		$orderRule = ($orderRule == 'price_asc') || ($orderRule == 'price_desc') ? 'price' : $orderRule;

		$like = strlen($searchKeyWord) == 0 ? "" : (strlen($condition) == 0 ? "WHERE CONCAT(food_name,food_style,food_flavor) LIKE '%{$searchKeyWord}%'" : "AND CONCAT(food_name,food_style,food_flavor) LIKE '%{$searchKeyWord}%'");

		$sql = "SELECT *, ( 6371 * acos( cos( radians('{$latitude}') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('{$longitude}') ) + sin( radians('{$latitude}') ) * sin( radians( latitude ) ) ) ) AS distance FROM food {$condition}{$like} ORDER BY {$orderRule} {$order} LIMIT {$start}, {$perPageNum}";
		// echo $sql;
		// exit();

		$res = Db::query($sql);

		for ($i = 0, $count = count($res); $i < $count; $i++) {
			$shopId = $res[$i]['shop_id'];
			$shopInfo = model('Shop')->where('id', $shopId)->find();
			$res[$i]['shop_name'] = $shopInfo->shop_name;
			$res[$i]['logo_url'] = $shopInfo->logo_url;
			$res[$i]['is_collect'] = model('CollectFood')->where(['food_id' => $res[$i]['id'], 'openid' => $openid])->find() ? 1 : 0; 
		}

		echo json_encode($res);
		exit();

	}


	// 获取所有菜品类别
	public function getFoodStyles() {
		$res = array('code' => 0);
		$foodStyles = model('FoodStyles');
		$data = $foodStyles -> select();
		if (count($data) > 0) {
			$res['code'] = 1;
			$res['data'] = $data;
		}
		echo json_encode($res);
		exit();
	}

}